package model;

import java.util.Random;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

/*****************************************
 * logManager
 * 
 * Responsible for setting up the logs and words in the level
 * as well as proving access to the log objects
 * 
 * @author Stephen Gregory, Jun Wang
 *
 */

/**
 * Constructor: logManager
 * Purpose: import words, create logs and assign random words to logs
 */

public class logManager {
	
	private ArrayList<String> word = new ArrayList<String>();
	protected ArrayList<log> logs = new ArrayList<log>();
	private Random generator = new Random(System.currentTimeMillis());	
	
public logManager(String d) {
		
		importWords(d); 
		
		//need to check that first letter of logs in same row do not match
		//currently checks to see if same letter in entire word list
		
		int index;
		
		for (int i = 0; i < 25; i++) {
			index = generator.nextInt(word.size());
			
			while (wordCheck(word.get(index).charAt(0))) {
				index = generator.nextInt(word.size());
			}
			
			logs.add(new log(word.get(index)));
			
			logs.get(i).setDim(100, 40);
			
			//set positions of logs
			logs.get(i).setPos((i%5)*230+100, (i/5+1)*80+30); //FIX
			
			//set velocity of logs based on row
			//rows in first row move right, rows in second row move left, etc
			logs.get(i).setVelocityX((int) (Math.pow(-1,i/5)*1) ); //change to 10
		}
		
		//final log
		index = generator.nextInt(word.size());
		
		while (wordCheck(word.get(index).charAt(0))) {
			index = generator.nextInt(word.size());
		}
		
		logs.add(new log(word.get(index)));
		logs.get(25).setDim(100, 40);
		logs.get(25).setPos(500, 20);
		
		index = generator.nextInt(23);
		int type = generator.nextInt(5);
		type = type%5;
		type++;
		logs.get(index).setType(type);
		
//		testPrintInfo();
		}

public logManager(String d, int speed) {
	
	importWords(d); 
	
	//need to check that first letter of logs in same row do not match
	//currently checks to see if same letter in entire word list
	
	int index;
	
	for (int i = 0; i < 25; i++) {
		index = generator.nextInt(word.size());
		
		while (wordCheck(word.get(index).charAt(0))) {
			index = generator.nextInt(word.size());
		}
		
		logs.add(new log(word.get(index)));
		
		logs.get(i).setDim(100, 40);
		
		//set positions of logs
		logs.get(i).setPos((i%5)*235, (i/5+1)*80+30);
		
		//set velocity of logs based on row
		//rows in first row move right, rows in second row move left, etc
		logs.get(i).setVelocityX((int) (Math.pow(-1,i/5)*speed) );
	}
	
	//final log
	index = generator.nextInt(word.size());
	logs.add(new log(word.get(index)));
	logs.get(25).setDim(100, 40);
	logs.get(25).setPos(500, 20);
	
	index = generator.nextInt(23);
	int type = generator.nextInt(5);
	type = type%5;
	type++;
	logs.get(index).setType(type);
	
	}

	
	private boolean wordCheck(char c) {
		boolean match = false;
		for (int i = 0; i < logs.size(); i++) {
			if (logs.get(i).getWord().charAt(0) == c) {
				match = true;
				break;
			}
		}
		return match;
	}
		
		
	/**
	 * Method: importWords
	 * Purpose: Import words from a text file
	 */
	
	private void importWords(String d) {
		
	    FileReader reader;
	    
		try {
			reader = new FileReader(d);
		    Scanner in = new Scanner(reader);

		    while (in.hasNextLine()) {
		    	String line = in.nextLine();
		    	word.add(line);
		    }		
		    in.close();		    
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		

	}
	
	/**
	 * Method: moveLogs
	 * Purpose: change the position of the logs
	 */
	
	public void moveLogs(int time) {
		for (int i = 0; i < logs.size()-1; i++)
			logs.get(i).move(time);
	}
		
	/**
	 * Method: getLogList
	 * Purpose: returns a pointer to the list of logs
	 */

	public  ArrayList<log> getLogList() {
		return logs;
	}
	
	public void incVel() {
		for (int i = 0; i < logs.size()-1; i++) {
			logs.get(i).incVelocityX();
		}
	}
	
	public void decVel() {
		for (int i = 0; i < logs.size()-1; i++) {
			logs.get(i).decVelocityX();
		}
	}

	/******************METHODS FOR TESTING*****************/
	
	/**
	 * Method: viewLogWords
	 * Purpose: outputs words in level for testing purposes
	 */
	
	private void viewLogWords() {
		System.out.println("[-----RANDOM WORDS-----]");
		for (int i = 0; i < logs.size(); i++) {
			System.out.println(logs.get(i).getWord());
		}
	}
	
	/**
	 * Method: testPrintInfo
	 * Purpose: check log contents for testing purposes
	 */
	//for 15, moving left, when positive x, position changes by 2, 
	//                     when negative x, position changes by 1
	
	//for 3, moving right, when positive x, position changes by 1, 
	//                     when negative x, position changes by 2
	private void testPrintInfo() {
		for (int i = 0; i <25 ; i++) 
			System.out.println("index: " + i + ", Type: "+ logs.get(i).getType() + " PosX: " + logs.get(i).getPosX() + ", PosY: " + logs.get(i).getPosY() + ", VelX: " + logs.get(i).getVelocityX() + ", Word: " + logs.get(i).getWord());		
	}


}
